﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using Common.Logging;
using SupportLibrary;
using SupportLibrary.Message;

namespace Automation.Dal.Excel.Dao
{
    public class TestConfigDao:TestConfigBase
    {
        private ILog log = LogManager.GetLogger(typeof(TestConfigDao));
         private ExcelProvider _exProvider;
        private string _Path;
        public TestConfigDao(string path)
        {
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
                //MessageShow.FileDoesNotExist(path);
                throw new Exception(path + "doesnot exist!");
        }
        //this function is added by SonNguyen, not ogriginal framework.
        public override Dictionary<string, string> getTestConfig()
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            
            try
            {
                if (_exProvider != null)
                {
                    var TestConfigs = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestConfig>();
                    int i = 0;
                    if (TestConfigs != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestConfig item in TestConfigs)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                {
                                    list.Add(item.Name, item.Value);
                                }
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    throw new Exception("Record " + (i + 1) + " is invalid!");
                                }
                            }
                        }
                    }
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));
                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TConfig sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadTConfigSheetErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadTConfigSheetErr + e.GetType().ToString());
            }

            return list;
        }

        // This function is added by QuangNguyen to get List TConfig
        public override List<TestConfig> getTestConfigs()
        {
            List<TestConfig> list = new List<TestConfig>();

            try
            {
                log.Debug(Resources.Information.msg_ReadingSheetTConfig);
                if (_exProvider != null)
                {
                    var TestConfigs = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestConfig>();
                    int i = 0;
                    if (TestConfigs != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestConfig item in TestConfigs)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                {
                                    list.Add(new TestConfig() { 
                                        ID = item.ID,
                                        Name = item.Name,
                                        Value = item.Value
                                    });
                                }
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    log.Debug(Resources.ErrorMessage.ReadTConfigSheetErr);
                                    throw new Exception("Record "+(i+1)+" is invalid!");
                                }
                            }
                        }
                    }
                   // Console.WriteLine(i + " records were read in sheet TConfig.");
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));
                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TConfig sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadTConfigSheetErr + e.GetType().ToString() + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadTConfigSheetErr + e.GetType().ToString());
            }

            return list;
        }
    }
}
